from exlib.restviewset.reqparse import RequestParser
from exlib.restplus.resource import ListResource, SingleResource
from app.enterprise.models import EnterPrise
from app.EnterPrise.serializers import CompanySerializer
from app import api_plus as api
from app.perms import COMAPNY_BASE, VIEW, ADD, EDIT, DELETE
from utils.decorators import marshal_item, marshal_table
from utils.perm import perm_required
from utils import pagination, get_pagination_arguments, get_object_or_404


# from utils import current_company


class CompanyView(SingleResource):
    model_class = EnterPrise
    model_serializer = CompanySerializer

    @perm_required(COMAPNY_BASE, VIEW)
    @marshal_item(CompanySerializer)
    def get(self, id):
        obj = get_object_or_404(EnterPrise, name=id)
        return CompanySerializer(obj).item()

    poster = RequestParser()
    poster.add_argument('name', type=str, required=False, help='公司名')
    poster.add_argument('vname', type=str, required=False, store_missing=False, help='公司中文名')
    poster.add_argument('desc', type=str, required=False, store_missing=False, help='描述')
    poster.add_argument('contact_info', type=str, required=False, store_missing=False, help='联系方式')

    @perm_required(COMAPNY_BASE, EDIT)
    @api.expect(poster)
    @marshal_item(CompanySerializer)
    def put(self, id):
        obj = get_object_or_404(EnterPrise, name=id)
        args = self.poster.parse_args()
        if args:
            obj.modify(**args)
            obj.save()
        return CompanySerializer(obj).item(
            message=self.msg_edit_success
        )

    @perm_required(COMAPNY_BASE, DELETE)
    @marshal_item(CompanySerializer)
    def delete(self, id):
        obj = get_object_or_404(EnterPrise, name=id)
        obj.is_deleted = True
        obj.save()
        return CompanySerializer(obj).item(
            message=self.msg_del_success
        )


class CompanysView(ListResource):
    model_class = EnterPrise
    model_serializer = CompanySerializer

    filter = RequestParser()
    filter.add_argument('id', type=str, required=False, store_missing=False, help='EnterPrise id')

    @perm_required(COMAPNY_BASE, VIEW)
    @api.expect(get_pagination_arguments(order_fields=('order_fields', '-created_time')), filter)
    @marshal_table(CompanySerializer)
    def get(self):
        args = self.filter.parse_args()
        objs = EnterPrise.objects.filter(**args).filter(type__ne="root")
        pargs = pagination.parse_args()
        res = self.order_paginate(objs, pargs)
        return CompanySerializer(res).table(
            total=objs.count(),
            pargs=pargs
        )

    # poster = RequestParser()
    # poster.add_argument('name', type=str, required=False, help='公司名')
    # poster.add_argument('desc', type=str, required=False, store_missing=False, help='描述')
    #
    # @perm_required(COMAPNY_BASE, ADD)
    # @marshal_item(CompanySerializer)
    # def post(self):
    #     args = self.filter.parse_args()
    #     obj = self.model_class(**args)
    #     obj.save()
    #     return CompanySerializer(obj).item(
    #         message=self.msg_add_success
    #     )
